import sys
from PyQt5.QtWidgets import QWidget, QTableWidget, QHBoxLayout, QApplication, QTableWidgetItem,QHeaderView,QAbstractItemView,QVBoxLayout,QPushButton
from PyQt5.QtGui import QStandardItem
from .word_memory import get_collection_record,get_v_name_by_vid

class CollectionTableWidget(QWidget):
    def __init__(self):
        super().__init__()
        self.record_list  = []
        # self.initUI()
    def show(self) -> None:
        super(CollectionTableWidget, self).show()
        get_collection_record()
        self.initUI()
    def initUI(self):

        self.setWindowTitle("单词记忆记录")
        self.resize(350,450)
        conLayout = QHBoxLayout()
        tableWidget = QTableWidget()

        conLayout.addWidget(tableWidget)
        tableWidget.setHorizontalHeaderLabels(['单词', '收藏时间', '  '])
        self.record_list = get_collection_record()
        # self.model = QStandardItemModel(len(record_list), 4)
        print(len(self.record_list))
        tableWidget.setRowCount(len(self.record_list))
        tableWidget.setColumnCount(3)
        for row in range(len(self.record_list)):
            record_tuple = self.record_list[row]
            for column in range(3):
                if column == 0:
                    name = get_v_name_by_vid(record_tuple[column+2])
                    if name is None:
                        break
                    else:
                        item = QTableWidgetItem(name)
                        tableWidget.setItem(row, column, item)
                if column ==1:
                    item = QTableWidgetItem(record_tuple[column+2])
                    tableWidget.setItem(row, column, item)
                if column == 2:
                    str = "取消收藏"+record_tuple[2]
                    remove_collect_button = QPushButton(str)
                    # remove_collect_button.setStyleSheet("QPushButton{margin:3px};")
                    remove_collect_button.clicked.connect(self.delete_clicked)  # 绑定按钮点击事件

                    tableWidget.setCellWidget(row,column,remove_collect_button)

        self.setLayout(conLayout)

    def delete_clicked(self):

        button = self.sender()

        # if button:
        #     row = self.table.indexAt(button.pos()).row()
        #     print(row)
            # self.table.removeRow(row)

if __name__ == '__main__':
    pass
    # app = QApplication(sys.argv)
    # table = MyTableWidget()
    # table.show()
    # sys.exit(app.exec_())
